﻿package baidu.reader.txt.ui.component
{
    import baidu.reader.common.*;
    import baidu.reader.common.model.*;
    import baidu.ui.controls.*;
    import baidu.ui.managers.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;

    public class SettingsPanel extends Sprite
    {
        private var bg:SettingPanelBG;
        private var ok_button:Button;
        private var font_combobox:ComboBox;
        private var fontSize_combobox:ComboBox;
        private var leading_combox:ComboBox;
        private var _textFormat:TextFormat;
        private var prefenrenceModel:AppPreferenceModel;

        public function SettingsPanel()
        {
            this.prefenrenceModel = InstanceManager.createSingletonInstance(AppPreferenceModel);
            this.initUI();
            return;
        }// end function

        private function initUI() : void
        {
            this.filters = [new BlurFilter(0, 0, 0)];
            this.bg = new SettingPanelBG();
            addChild(this.bg);
            this.ok_button = new Button();
            this.ok_button.useHandCursor = true;
            this.ok_button.setStyle("skin", OKButtonSkin);
            addChild(this.ok_button);
            this.ok_button.x = -70;
            this.ok_button.y = 130;
            this.ok_button.addEventListener(MouseEvent.CLICK, this.okClickHandler);
            var _loc_1:* = new Label();
            _loc_1.textField.autoSize = TextFieldAutoSize.LEFT;
            _loc_1.text = "文字";
            addChild(_loc_1);
            _loc_1.x = -110;
            _loc_1.y = 30;
            this.font_combobox = new ComboBox();
            this.font_combobox.addEventListener(Event.CHANGE, this.fontChangeHandler);
            this.font_combobox.addItem({label:"宋体", value:"宋体,simsun,STSong"});
            this.font_combobox.addItem({label:"楷体", value:"楷体,KaiTi,STKaiti"});
            this.font_combobox.addItem({label:"微软雅黑", value:"微软雅黑,Microsoft YaHei,黑体,SimHei"});
            this.font_combobox.addItem({label:"黑体", value:"黑体,SimHei"});
            addChild(this.font_combobox);
            this.font_combobox.x = -70;
            this.font_combobox.y = 30;
            this.fontSize_combobox = new ComboBox();
            this.fontSize_combobox.addEventListener(Event.CHANGE, this.fontSizeChangeHandler);
            this.fontSize_combobox.addItem({label:"10号", value:10});
            this.fontSize_combobox.addItem({label:"12号", value:12});
            this.fontSize_combobox.addItem({label:"14号", value:14});
            this.fontSize_combobox.addItem({label:"16号", value:16});
            this.fontSize_combobox.addItem({label:"20号", value:20});
            this.fontSize_combobox.selectedIndex = 2;
            addChild(this.fontSize_combobox);
            this.fontSize_combobox.x = -70;
            this.fontSize_combobox.y = 60;
            var _loc_2:* = new Label();
            _loc_2.textField.autoSize = TextFieldAutoSize.LEFT;
            _loc_2.text = "行距";
            addChild(_loc_2);
            _loc_2.x = -110;
            _loc_2.y = 90;
            this.leading_combox = new ComboBox();
            this.leading_combox.addEventListener(Event.CHANGE, this.leadingChangeHandler);
            this.leading_combox.addItem({label:"8点", value:8});
            this.leading_combox.addItem({label:"12点", value:12});
            this.leading_combox.addItem({label:"16点", value:16});
            addChild(this.leading_combox);
            this.leading_combox.x = -70;
            this.leading_combox.y = 90;
            this.setComboBoxValue(this.font_combobox, this.prefenrenceModel.fontName);
            this.setComboBoxValue(this.fontSize_combobox, this.prefenrenceModel.fontSize);
            this.setComboBoxValue(this.leading_combox, this.prefenrenceModel.leading);
            return;
        }// end function

        private function setComboBoxValue(param1:ComboBox, param2) : void
        {
            var _loc_3:int = 0;
            while (_loc_3 < param1.dataProvider.length)
            {
                
                if (param1.dataProvider.getItemAt(_loc_3).value == param2)
                {
                    param1.selectedIndex = _loc_3;
                    break;
                }
                _loc_3++;
            }
            return;
        }// end function

        private function calculateFormat() : void
        {
            this._textFormat = new TextFormat();
            this._textFormat.font = this.font_combobox.selectedItem.value;
            this._textFormat.size = this.fontSize_combobox.selectedItem.value;
            this._textFormat.leading = this.leading_combox.selectedItem.value;
            return;
        }// end function

        private function leadingChangeHandler(event:Event) : void
        {
            this.calculateFormat();
            this.prefenrenceModel.leading = int(this._textFormat.leading);
            dispatchEvent(new ReaderEvent(ReaderEvent.TEXT_FORMAT_CHANGED, this._textFormat));
            return;
        }// end function

        private function fontSizeChangeHandler(event:Event) : void
        {
            this.calculateFormat();
            this.prefenrenceModel.fontSize = int(this._textFormat.size);
            dispatchEvent(new ReaderEvent(ReaderEvent.TEXT_FORMAT_CHANGED, this._textFormat));
            return;
        }// end function

        private function fontChangeHandler(event:Event) : void
        {
            this.calculateFormat();
            this.prefenrenceModel.fontName = this._textFormat.font;
            dispatchEvent(new ReaderEvent(ReaderEvent.TEXT_FORMAT_CHANGED, this._textFormat));
            return;
        }// end function

        private function okClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.CLOSE_SETTING_PANEL));
            return;
        }// end function

        public function get textFormat() : TextFormat
        {
            return this._textFormat;
        }// end function

    }
}
